package com.accloud.cloudservice;

import android.util.Base64;
import com.accloud.common.ACConfiguration;
import com.accloud.service.ACDeviceGroup;
import com.accloud.service.ACDeviceMsg;
import com.accloud.service.ACDeviceTask;
import com.accloud.service.ACDeviceTimerMgr;
import com.accloud.service.ACException;
import com.accloud.service.ACMsg;
import com.accloud.service.ACObject;
import cz.msebera.android.httpclient.entity.mime.MIME;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.TimeZone;

public class ACDeviceTimerManager extends ACBaseManager implements ACDeviceTimerMgr {
    private long deviceId;
    private TimeZone timeZone;

    public ACDeviceTimerManager(long deviceId2) {
        this(deviceId2, TimeZone.getDefault());
    }

    public ACDeviceTimerManager(long deviceId2, TimeZone timeZone2) {
        super(ACConfiguration.TIMER_SERVICE_NAME, 1, false);
        this.deviceId = deviceId2;
        this.timeZone = timeZone2;
    }

    @Override // com.accloud.service.ACDeviceTimerMgr
    public void addTask(ACDeviceTask task, PayloadCallback<ACDeviceTask> callback) {
        final PayloadCallback<ACDeviceTask> callbackWrapper = new PayloadEventCallback<>(new ACTaskEvent("create", String.valueOf(task.getTaskId()), "device", task.getName(), "single"), callback);
        if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("addCloudTask");
        req.put("task", getObjFromTask(task));
        try {
            req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
        } catch (IOException e) {
        }
        sendReq(req, callbackWrapper, new ACMsgListener() {
            /* class com.accloud.cloudservice.ACDeviceTimerManager.AnonymousClass1 */

            @Override // com.accloud.cloudservice.ACMsgListener
            public void finish(ACMsg resp) {
                try {
                    ACObjectMarshaller.unmarshal(resp, resp.getPayload());
                    callbackWrapper.success(ACDeviceTimerManager.this.getTaskFromObj((ACObject) resp.get("task")));
                } catch (IOException e) {
                    callbackWrapper.error(new ACException(ACException.MARSHAL_ERROR, e.toString()));
                }
            }
        });
    }

    @Override // com.accloud.service.ACDeviceTimerMgr
    public void modifyTask(ACDeviceTask task, VoidCallback callback) {
        VoidCallback callbackWrapper = new VoidEventCallback(new ACTaskEvent("modify", String.valueOf(task.getTaskId()), "device", task.getName(), "single"), callback);
        if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("updateCloudTask");
        req.put("taskId", Long.valueOf(task.getTaskId()));
        req.put("task", getObjFromTask(task));
        try {
            req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
        } catch (IOException e) {
        }
        sendReq(req, callbackWrapper);
    }

    @Override // com.accloud.service.ACDeviceTimerMgr
    public void openTask(long taskId, VoidCallback callback) {
        VoidCallback callbackWrapper = new VoidEventCallback(new ACTaskEvent("start", String.valueOf(taskId), "device", null, "single"), callback);
        if (taskId <= 0) {
            callbackWrapper.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else {
            ACMsg req = ACMsg.getACMsg("");
            req.setName("enableCloudTask");
            req.put("taskId", Long.valueOf(taskId));
            try {
                req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
            } catch (IOException e) {
            }
            sendReq(req, callbackWrapper);
        }
    }

    @Override // com.accloud.service.ACDeviceTimerMgr
    public void closeTask(long taskId, VoidCallback callback) {
        VoidCallback callbackWrapper = new VoidEventCallback(new ACTaskEvent("stop", String.valueOf(taskId), "device", null, "single"), callback);
        if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("disableCloudTask");
        req.put("taskId", Long.valueOf(taskId));
        try {
            req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
        } catch (IOException e) {
        }
        sendReq(req, callbackWrapper);
    }

    @Override // com.accloud.service.ACDeviceTimerMgr
    public void deleteTask(long taskId, VoidCallback callback) {
        VoidCallback callbackWrapper = new VoidEventCallback(new ACTaskEvent("delete", String.valueOf(taskId), "device", null, "single"), callback);
        if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("delCloudTask");
        req.put("taskId", Long.valueOf(taskId));
        try {
            req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
        } catch (IOException e) {
        }
        sendReq(req, callbackWrapper);
    }

    @Override // com.accloud.service.ACDeviceTimerMgr
    public void listTasks(final PayloadCallback<List<ACDeviceTask>> callback) {
        if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("listCloudTasksByOwner");
        req.put("ownerType", 1);
        req.put("ownerId", Long.valueOf(this.deviceId));
        try {
            req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
        } catch (IOException e) {
        }
        sendReq(req, callback, new ACMsgListener() {
            /* class com.accloud.cloudservice.ACDeviceTimerManager.AnonymousClass2 */

            @Override // com.accloud.cloudservice.ACMsgListener
            public void finish(ACMsg resp) {
                try {
                    ACObjectMarshaller.unmarshal(resp, resp.getPayload());
                    callback.success(ACDeviceTimerManager.this.getTasksFromObjs((List) resp.get("tasks")));
                } catch (IOException e) {
                    callback.error(new ACException(ACException.MARSHAL_ERROR, e.toString()));
                }
            }
        });
    }

    @Override // com.accloud.service.ACDeviceTimerMgr
    public void addTaskGroup(ACDeviceGroup group, PayloadCallback<ACDeviceGroup> callback) {
        final PayloadCallback<ACDeviceGroup> callbackWrapper = new PayloadEventCallback<>(new ACTaskEvent("create", group.getGroupId(), "device", group.getGroupName(), "group"), callback);
        if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else if (group.getTasks() == null || group.getTasks().size() <= 0) {
            callbackWrapper.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else {
            ACMsg req = ACMsg.getACMsg("");
            req.setName("addTaskGroup");
            req.put("tasks", getObjsFromTasks(group.getTasks()));
            req.put("groupName", group.getGroupName());
            try {
                req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
            } catch (IOException e) {
            }
            sendReq(req, callbackWrapper, new ACMsgListener() {
                /* class com.accloud.cloudservice.ACDeviceTimerManager.AnonymousClass3 */

                @Override // com.accloud.cloudservice.ACMsgListener
                public void finish(ACMsg resp) {
                    try {
                        ACObjectMarshaller.unmarshal(resp, resp.getPayload());
                    } catch (IOException e) {
                    }
                    ACObject object = (ACObject) resp.get("group");
                    callbackWrapper.success(new ACDeviceGroup((String) object.get("id"), (String) object.get("name"), ACDeviceTimerManager.this.getTasksFromObjs((List) object.get("tasks"))));
                }
            });
        }
    }

    @Override // com.accloud.service.ACDeviceTimerMgr
    public void modifyTaskGroup(ACDeviceGroup group, VoidCallback callback) {
        VoidCallback callbackWrapper = new VoidEventCallback(new ACTaskEvent("modify", group.getGroupId(), "device", group.getGroupName(), "group"), callback);
        if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else if (group == null || group.getGroupId() == null || group.getTasks() == null || group.getTasks().size() <= 0) {
            callbackWrapper.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else {
            ACMsg req = ACMsg.getACMsg("");
            req.setName("updateTaskGroup");
            req.put("groupId", group.getGroupId());
            req.put("tasks", getObjsFromTasks(group.getTasks()));
            req.put("groupName", group.getGroupName());
            try {
                req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
            } catch (IOException e) {
            }
            sendReq(req, callbackWrapper);
        }
    }

    @Override // com.accloud.service.ACDeviceTimerMgr
    public void listTaskGroups(final PayloadCallback<List<ACDeviceGroup>> callback) {
        if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
        } else if (this.deviceId <= 0) {
            callback.error(new ACException(ACException.INVALID_PARAMETERS, "invalid parameters"));
        } else {
            ACMsg req = ACMsg.getACMsg("");
            req.setName("listTaskGroups");
            req.put("ownerId", Long.valueOf(this.deviceId));
            req.put("ownerType", 1);
            try {
                req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
            } catch (IOException e) {
            }
            sendReq(req, callback, new ACMsgListener() {
                /* class com.accloud.cloudservice.ACDeviceTimerManager.AnonymousClass4 */

                @Override // com.accloud.cloudservice.ACMsgListener
                public void finish(ACMsg resp) {
                    try {
                        ACObjectMarshaller.unmarshal(resp, resp.getPayload());
                    } catch (IOException e) {
                    }
                    List<ACObject> objects = (List) resp.get("groups");
                    ArrayList arrayList = new ArrayList();
                    if (objects != null) {
                        for (ACObject object : objects) {
                            arrayList.add(new ACDeviceGroup((String) object.get("id"), (String) object.get("name"), ACDeviceTimerManager.this.getTasksFromObjs((List) object.get("tasks"))));
                        }
                    }
                    callback.success(arrayList);
                }
            });
        }
    }

    @Override // com.accloud.service.ACDeviceTimerMgr
    public void getTaskGroupInfoById(String groupId, final PayloadCallback<ACDeviceGroup> callback) {
        if (!AC.accountMgr().isLogin()) {
            callback.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("getTaskGroup");
        req.put("groupId", groupId);
        try {
            req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
        } catch (IOException e) {
        }
        sendReq(req, callback, new ACMsgListener() {
            /* class com.accloud.cloudservice.ACDeviceTimerManager.AnonymousClass5 */

            @Override // com.accloud.cloudservice.ACMsgListener
            public void finish(ACMsg resp) {
                try {
                    ACObjectMarshaller.unmarshal(resp, resp.getPayload());
                } catch (IOException e) {
                }
                ACObject object = (ACObject) resp.get("group");
                callback.success(new ACDeviceGroup((String) object.get("id"), (String) object.get("name"), ACDeviceTimerManager.this.getTasksFromObjs((List) object.get("tasks"))));
            }
        });
    }

    @Override // com.accloud.service.ACDeviceTimerMgr
    public void deleteTaskGroup(String groupId, VoidCallback callback) {
        VoidCallback callbackWrapper = new VoidEventCallback(new ACTaskEvent("delete", groupId, "device", null, "group"), callback);
        if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("delTaskGroup");
        req.put("groupId", groupId);
        try {
            req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
        } catch (IOException e) {
        }
        sendReq(req, callbackWrapper);
    }

    @Override // com.accloud.service.ACDeviceTimerMgr
    public void openTaskGroup(String groupId, VoidCallback callback) {
        VoidCallback callbackWrapper = new VoidEventCallback(new ACTaskEvent("start", groupId, "device", null, "group"), callback);
        if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("enableTaskGroup");
        req.put("groupId", groupId);
        try {
            req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
        } catch (IOException e) {
        }
        sendReq(req, callbackWrapper);
    }

    @Override // com.accloud.service.ACDeviceTimerMgr
    public void closeTaskGroup(String groupId, VoidCallback callback) {
        VoidCallback callbackWrapper = new VoidEventCallback(new ACTaskEvent("stop", groupId, "device", null, "group"), callback);
        if (!AC.accountMgr().isLogin()) {
            callbackWrapper.error(new ACException(ACException.NO_LOGIN, "no login"));
            return;
        }
        ACMsg req = ACMsg.getACMsg("");
        req.setName("disableTaskGroup");
        req.put("groupId", groupId);
        try {
            req.setPayload(ACObjectMarshaller.marshal(req), "text/json");
        } catch (IOException e) {
        }
        sendReq(req, callbackWrapper);
    }

    private ACObject getObjFromTask(ACDeviceTask task) {
        ACObject object = new ACObject();
        object.put("name", task.getName());
        object.put("desc", task.getDescription());
        object.put("tag", task.getTag());
        ACObject timeRule = new ACObject();
        if (task.getTimeZone() != null) {
            timeRule.put("timeZone", task.getTimeZone());
        } else {
            timeRule.put("timeZone", this.timeZone.getID());
        }
        timeRule.put("timeCycle", task.getTimeCycle());
        timeRule.put("timePoint", task.getTimePoint());
        object.put("timeRule", timeRule);
        ACObject command = new ACObject();
        command.put("name", "device-command");
        ACObject deviceCmd = new ACObject();
        deviceCmd.put("deviceId", Long.valueOf(this.deviceId));
        deviceCmd.put("code", String.valueOf(task.getDeviceMsg().getCode()));
        deviceCmd.put(MIME.ENC_BINARY, task.getDeviceMsg().getContent());
        command.put("deviceCmd", deviceCmd);
        object.put("command", command);
        return object;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private ACDeviceTask getTaskFromObj(ACObject object) {
        ACDeviceTask task = new ACDeviceTask();
        long taskId = ((Long) object.get("id")).longValue();
        long status = object.getLong("status");
        task.setTaskId(taskId);
        task.setName((String) object.get("name"));
        task.setDescription((String) object.get("desc"));
        task.setTag((String) object.get("tag"));
        task.setStatus((int) status);
        ACObject timeRule = (ACObject) object.get("timeRule");
        task.setTimeZone((String) timeRule.get("timeZone"));
        task.setTimeCycle((String) timeRule.get("timeCycle"));
        task.setTimePoint((String) timeRule.get("timePoint"));
        ACObject command = (ACObject) object.get("command");
        if (((String) command.get("name")).equals("device-command")) {
            ACObject deviceCmd = (ACObject) command.get("deviceCmd");
            task.setDeviceMsg(new ACDeviceMsg(Integer.valueOf((String) deviceCmd.get("code")).intValue(), Base64.decode(deviceCmd.getString(MIME.ENC_BINARY), 0)));
        }
        task.setCreateTime(object.getString("createTime"));
        task.setModifyTime(object.getString("modifyTime"));
        return task;
    }

    private List<ACObject> getObjsFromTasks(List<ACDeviceTask> tasks) {
        List<ACObject> taskObjs = new ArrayList<>();
        for (ACDeviceTask task : tasks) {
            ACObject object = new ACObject();
            object.put("name", task.getName());
            if (task.getDescription() != null) {
                object.put("desc", task.getDescription());
            }
            object.put("tag", task.getTag());
            ACObject timeRule = new ACObject();
            if (task.getTimeZone() == null) {
                timeRule.put("timeZone", this.timeZone.getID());
            } else {
                timeRule.put("timeZone", task.getTimeZone());
            }
            timeRule.put("timeCycle", task.getTimeCycle());
            timeRule.put("timePoint", task.getTimePoint());
            object.put("timeRule", timeRule);
            ACObject command = new ACObject();
            ACObject deviceCommand = new ACObject();
            deviceCommand.put("deviceId", Long.valueOf(this.deviceId));
            deviceCommand.put("code", String.valueOf(task.getDeviceMsg().getCode()));
            deviceCommand.put(MIME.ENC_BINARY, task.getDeviceMsg().getContent());
            command.put("name", "device-command");
            command.put("deviceCmd", deviceCommand);
            object.put("command", command);
            taskObjs.add(object);
        }
        return taskObjs;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private List<ACDeviceTask> getTasksFromObjs(List<ACObject> objects) {
        List<ACDeviceTask> tasks = new ArrayList<>();
        if (objects != null) {
            for (ACObject object : objects) {
                ACDeviceTask task = new ACDeviceTask();
                long taskId = ((Long) object.get("id")).longValue();
                long status = ((Long) object.get("status")).longValue();
                ACObject timeRule = (ACObject) object.get("timeRule");
                task.setTaskId(taskId);
                task.setName((String) object.get("name"));
                task.setDescription((String) object.get("desc"));
                task.setTag((String) object.get("tag"));
                task.setTimeZone((String) timeRule.get("timeZone"));
                task.setTimeCycle((String) timeRule.get("timeCycle"));
                task.setTimePoint((String) timeRule.get("timePoint"));
                task.setStatus((int) status);
                ACObject command = (ACObject) object.get("command");
                if (((String) command.get("name")).equals("device-command")) {
                    ACObject deviceCmd = (ACObject) command.get("deviceCmd");
                    task.setDeviceMsg(new ACDeviceMsg(Integer.valueOf((String) deviceCmd.get("code")).intValue(), Base64.decode(deviceCmd.getString(MIME.ENC_BINARY), 0)));
                }
                task.setCreateTime(object.getString("createTime"));
                task.setModifyTime(object.getString("modifyTime"));
                tasks.add(task);
            }
        }
        return tasks;
    }
}
